﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MusicAcademy
{
    public partial class frmTeacherFreeTime : Form
    {
        public frmTeacherFreeTime()
        {
            InitializeComponent();
        }

        public int TeacherID = -1;
        private int recordID = -1;

        private void RefreshGrid()
        {
            fullTeacherFreeTimeTableAdapter.Fill(musicAcademyDataSet.FullTeacherFreeTime, TeacherID, 8);
        }

        private bool isValid()
        {
            bool valid = true;
            if (TeacherID==-1)
                valid = Globals.SetError(txtTeacher);
            else
                Globals.SetValid(txtTeacher);

            if (txtFromTime.Text.IndexOf("_") >= 0 || Globals.IsValidTime(txtFromTime.Text) == false)
                valid = Globals.SetError(txtFromTime);
            else
                Globals.SetValid(txtFromTime);

            if (txtToTime.Text.IndexOf("_") >= 0 || Globals.IsValidTime(txtToTime.Text) == false)
                valid = Globals.SetError(txtToTime);
            else
                Globals.SetValid(txtToTime);
            
            return valid;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            if (UserManager.HasAccess("frmTeacherFreeTime", "View") == false)
            {
                MessageBox.Show("شما اجازه ی دیدن این فرم را ندارید !", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.Close();
            }
            
            if (TeacherID > -1)
            {
                SqlConnection con = new SqlConnection(Properties.Settings.Default.MusicAcademyConnectionString);
                SqlCommand com = new SqlCommand();
                SqlDataReader dreader;

                com.Connection = con;
                com.CommandType = CommandType.Text;
                com.CommandText = "SELECT firstName, lastName FROM Person WHERE ID=@ID";
                com.Parameters.AddWithValue("@ID", TeacherID);

                con.Open();
                dreader = com.ExecuteReader();
                dreader.Read();
                txtTeacher.Text = dreader["firstName"].ToString() + " " + dreader["lastName"].ToString();
                dreader.Close();
                con.Close();
            }
            // TODO: This line of code loads data into the 'musicAcademyDataSet.WeekDays' table. You can move, or remove it, as needed.
            this.weekDaysTableAdapter.Fill(this.musicAcademyDataSet.WeekDays);
            cmbDOW.SelectedIndex = 0;
            RefreshGrid();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ListOfPersons p = new ListOfPersons();
            p.PersonType = 1;
            if (p.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                TeacherID = p.SelectedID;
                txtTeacher.Text = p.FirstName + " " + p.LastName;
                RefreshGrid();
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(Properties.Settings.Default.MusicAcademyConnectionString);
            SqlCommand com = new SqlCommand();

            com.Connection = con;
            com.CommandType = CommandType.Text;

            if (isValid() == false)
                return;

            if (recordID == -1)
            {
                if (UserManager.HasAccess("frmTeacherFreeTime", "Insert") == false)
                {
                    MessageBox.Show("شما اجازه ی اضافه کردن رکورد را ندارید !", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                com.CommandText = "INSERT INTO TeacherFreeTime (teacherID, DOW, fromTime, toTime, creator, createDate) VALUES (@teacherID, @DOW, @fromTime, @toTime, @creator, @createDate)";
                com.Parameters.AddWithValue("@teacherID", TeacherID);
                com.Parameters.AddWithValue("@DOW", cmbDOW.SelectedValue);
                com.Parameters.AddWithValue("@fromTime", txtFromTime.Text);
                com.Parameters.AddWithValue("@toTime", txtToTime.Text);
                com.Parameters.AddWithValue("@creator", Program.userID);
                com.Parameters.AddWithValue("@createDate", Globals.Get10CharDate());

                con.Open();
                com.ExecuteNonQuery();
                con.Close();
                txtFromTime.Text = "";
                txtToTime.Text = "";

                RefreshGrid();
            }
            else
            {
                if (UserManager.HasAccess("frmTeacherFreeTime", "Update") == false)
                {
                    MessageBox.Show("شما اجازه ی ویرایش کردن رکورد را ندارید !", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                com.CommandText = "UPDATE TeacherFreeTime SET DOW=@DOW, fromTime=@fromTime, toTime=@toTime, lastEditor=@lastEditor, editDate=@editDate WHERE ID=@ID";
                com.Parameters.AddWithValue("@ID", recordID);
                com.Parameters.AddWithValue("@DOW", cmbDOW.SelectedValue);
                com.Parameters.AddWithValue("@fromTime", txtFromTime.Text);
                com.Parameters.AddWithValue("@toTime", txtToTime.Text);
                com.Parameters.AddWithValue("@lastEditor", Program.userID);
                com.Parameters.AddWithValue("@editDate", Globals.Get10CharDate());

                con.Open();
                com.ExecuteNonQuery();
                con.Close();
                txtFromTime.Text = "";
                txtToTime.Text = "";

                RefreshGrid();
            }

            MessageBox.Show("ذخیره شد", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void GrdFreeTime_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1)
                return;

            recordID = Convert.ToInt32(GrdFreeTime.Rows[e.RowIndex].Cells["ID"].Value);
            txtTeacher.Text = GrdFreeTime.Rows[e.RowIndex].Cells["firstName"].Value.ToString() + " " + GrdFreeTime.Rows[e.RowIndex].Cells["lastName"].Value.ToString();
            TeacherID = Convert.ToInt32(GrdFreeTime.Rows[e.RowIndex].Cells["_teacherID"].Value);
            cmbDOW.SelectedValue = Convert.ToByte(GrdFreeTime.Rows[e.RowIndex].Cells["DOW"].Value);
            txtFromTime.Text = GrdFreeTime.Rows[e.RowIndex].Cells["fromTime"].Value.ToString();
            txtToTime.Text = GrdFreeTime.Rows[e.RowIndex].Cells["toTime"].Value.ToString();
            button1.Enabled = false;
            btnDelete.Enabled = true;

            Globals.SetValid(txtTeacher);
            Globals.SetValid(txtFromTime);
            Globals.SetValid(txtToTime);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            ClearForm();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (UserManager.HasAccess("frmTeacherFreeTime", "Delete") == false)
            {
                MessageBox.Show("شما اجازه ی حذف کردن رکورد را ندارید !", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            if (MessageBox.Show("آیا از این حذف رکورد اطمینان دارید ؟", "حذف", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {

                SqlConnection con = new SqlConnection(Properties.Settings.Default.MusicAcademyConnectionString);
                SqlCommand com = new SqlCommand();
                com.Connection = con;
                com.CommandType = CommandType.Text;
                com.CommandText = "DELETE FROM TeacherFreeTime WHERE ID=@ID";
                com.Parameters.AddWithValue("@ID", recordID);
                con.Open();
                com.ExecuteNonQuery();
                con.Close();

                ClearForm();

                MessageBox.Show("حذف شد", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void ClearForm()
        {
            button1.Enabled = true;
            btnDelete.Enabled = false;
            txtFromTime.Text = "";
            txtToTime.Text = "";
            txtTeacher.Text = "";
            TeacherID = -1;
            recordID = -1;
            RefreshGrid();
            GrdFreeTime.ClearSelection();
            cmbDOW.SelectedIndex = 0;

            Globals.SetValid(txtTeacher);
            Globals.SetValid(txtFromTime);
            Globals.SetValid(txtToTime);
        }
    }
}
